package com.itlxd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itlxd.dto.PageDTO;
import com.itlxd.entity.Category;
import com.itlxd.exception.CategoryException;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.mapper.CategoryMapper;
import com.itlxd.mapper.DishMapper;
import com.itlxd.mapper.SetmealMapper;
import com.itlxd.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itlxd.service.IDishService;
import com.itlxd.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public IPage<Category> listPageCategories(PageDTO pageDTO) {
        IPage<Category> page = new Page(pageDTO.getPage(), pageDTO.getPageSize());
        LambdaQueryWrapper<Category> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.like(null != pageDTO.getName(), Category::getName, pageDTO.getName())
                .orderByAsc(Category::getType)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime);
        return categoryMapper.selectPage(page, lambdaQuery);
    }

    @Override
    public Integer saveCategory(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public Integer updateCategory(Category category) {
        return categoryMapper.updateById(category);
    }

    @Override
    public Integer removeById(Long id) {
        Integer count = categoryMapper.countDishAndSetmeal(id);
        if (count > SysConstants.NUM_ZERO) {
            throw new CategoryException(SysMessages.CATEGORY_HAS_DISH_OR_SETMEAL);
        }

        return categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> listCategories(Category category) {
        Object obj = redisTemplate.opsForValue().get("categoryList");
        if (null != obj) {
            List list = JSON.parseObject(obj.toString(), List.class);
            return list;
        }

        LambdaQueryWrapper<Category> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(null != category.getType(), Category::getType, category.getType())
                .orderByAsc(Category::getType)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime);
        List<Category> categories = categoryMapper.selectList(lambdaQuery);
        redisTemplate.opsForValue().set("categoryList", JSON.toJSON(categories), 60L, TimeUnit.MINUTES);

        return categories;
    }

    @Override
    public Integer countDishAndSetmeal(Long id) {
        return categoryMapper.countDishAndSetmeal(id);
    }

    @Override
    public Category getById(Long id) {
        return categoryMapper.selectById(id);
    }
}
